package practise.leetcode.q13;

public class Solution {
    private String[] romans = new String[] {
            "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
    };

    private int[] nums = new int[] {
            1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
    };

    public int romanToInt(String s) {
        int num = 0;
        int startIdx = 0;
        while (!s.isEmpty()) {
            for (int i = startIdx; i < romans.length; i++) {
                String roman = romans[i];
                if (s.substring(0, 1).equals(roman)) {
                    num += nums[i];
                    s = s.substring(1);
                    break;
                }
                else if (s.length() >= 2 && s.substring(0, 2).equals(roman)) {
                    num += nums[i];
                    s = s.substring(2);
                    break;
                }
                startIdx++;
            }
        }
        return num;
    }

    String getSubstring(int startIdx, int endIdx, String s) {
        return s.substring(startIdx, endIdx);
    }

    String getSubstring(int startIdx, String s) {
        return s.substring(startIdx);
    }
}
